package nc.ui.zzjs.bd.chnlarea;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.bs.uap.bd.BDRuntimeException;
import nc.itf.uap.IUAPQueryBS;
import nc.ui.pub.ClientEnvironment;
import nc.ui.trade.pub.IVOTreeDataByID;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.zzjs.bd.chnlarea.ChnlAreaVO;

public class ChnlAreaTreeCardData implements IVOTreeDataByID {
	
	private SuperVO[] m_ChnlAreaVOs = null;

	public String getIDFieldName() {
		// TODO Auto-generated method stub
		return "pk_chnlarea";
	}

	public String getParentIDFieldName() {
		// TODO Auto-generated method stub
		return "pk_parent";
	}

	public String getShowFieldName() {
		// TODO Auto-generated method stub
		return "vname";
	}

	public SuperVO[] getTreeVO() {
		if (m_ChnlAreaVOs == null) {
			IUAPQueryBS queryBS = (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
			Collection coll = null;
			try {
				StringBuffer strWhere = new StringBuffer(
						"(pk_corp='0001' or pk_corp='").append(ClientEnvironment.getInstance().getCorporation().getPrimaryKey()).append("')");
				coll = queryBS.retrieveByClause(ChnlAreaVO.class, strWhere.toString());
			} catch (BusinessException e) {
				Logger.error(e.getMessage(), e);
				throw new BDRuntimeException(e.getMessage(), e);
			}
			if (coll == null)
				return null;
			m_ChnlAreaVOs = (ChnlAreaVO[]) coll.toArray(new ChnlAreaVO[0]);
		}
		Arrays.sort(m_ChnlAreaVOs, 
			new Comparator() {
				public int compare(Object o1, Object o2) {
					String code1 = ((ChnlAreaVO) o1).getVcode();
					String code2 = ((ChnlAreaVO) o2).getVcode();
					return code1.compareTo(code2);
				}
			}
		);
		return m_ChnlAreaVOs;
	}

}
